package com.wnxy.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wnxy.admin.dto.ImagesDto;
import com.wnxy.admin.entity.MaintenanceTasks;
import com.wnxy.admin.entity.TaskImages;
import com.wnxy.admin.mapper.MaintenanceTasksMapper;
import com.wnxy.admin.mapper.TaskImagesMapper;
import com.wnxy.admin.service.ITaskImagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@Service
@RequiredArgsConstructor
public class TaskImagesServiceImpl extends ServiceImpl<TaskImagesMapper, TaskImages> implements ITaskImagesService {

    private final TaskImagesMapper taskImagesMapper;
    private final RedisTemplate<String, String> redisTemplate;
    private final MaintenanceTasksMapper maintenanceTasksMapper;

    @Override
    @Transactional
    public void processingDetails(ImagesDto imagesDto) {
        // 路径集合
        List<Object> trails = imagesDto.getTrails();
        if (trails.isEmpty()) {
            return;
        }

        // 任务id
        Long taskNumber = imagesDto.getTaskNumber();
        //判断任务状态,已处理任务不追加数据
        MaintenanceTasks maintenanceTasks = maintenanceTasksMapper.selectById(taskNumber);
        if(!ObjectUtils.isEmpty(maintenanceTasks) && maintenanceTasks.getStatus() != -1 ) {
            return;
        }
        // 遍历添加到数据库
        trails.forEach(e -> {
            if (e != null) {
                TaskImages taskImages = new TaskImages();
                taskImages.setTaskNumber(taskNumber);
                taskImages.setImagePath(e.toString());

                // 存入redis
                redisTemplate.opsForSet().add("save-task-file-db", e.toString());
                taskImagesMapper.insert(taskImages);
            }
        });
    }
}
